package thread;

import java.util.concurrent.CountDownLatch;

/**
 * @Description: CountDownLatch 用法
 * @Project: testDemo
 * @Author: hjibo
 * @CreateDate: 2019/2/21 18:16
 * @ModificationHistory: （who whatTime doWhat）
 */
public class ThreadBath implements Runnable {

    private String name;// 姓名
    private CountDownLatch worm;// 小弟
    private CountDownLatch mogul;// 大佬

    public ThreadBath(String name, CountDownLatch worm, CountDownLatch mogul) {
        this.name = name;
        this.worm = worm;
        this.mogul = mogul;
    }

    @Override
    public void run() {
        try {
            System.out.println(this.name+"：想要洗澡");
            if(worm != null){
                    worm.await();
            }
            System.out.println(this.name+"：进入澡堂子洗澡了。。。");
            Thread.sleep(3000);
            System.out.println(this.name+"：洗完澡了！");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if(mogul != null && mogul.getCount() != 0){
                mogul.countDown();
            }
        }
    }
}
